package gui.tab;

import gui.TuristAssistant;
import gui.dialog.CommSendEMailDialog;
import gui.pop.EMailPopUpMenu;

import java.util.Vector;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MenuDetectEvent;
import org.eclipse.swt.events.MenuDetectListener;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Shell;
import org.example.www.communication.MailService.EMail;

/**
 * This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI
 * Builder, which is free for non-commercial use. If Jigloo is being used
 * commercially (ie, by a corporation, company or business for any purpose
 * whatever) then you should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details. Use of Jigloo implies
 * acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN
 * PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR
 * ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
/**
 * El compiste a utilizar como solapa en la aplicacion para interactuar con los
 * servicios de e-mail.
 */
public class EMailCompositeTab extends org.eclipse.swt.widgets.Composite {
	private Vector<EMail>	emails;
	private EMailPopUpMenu	emailsPupupMenu;
	
	private Button			swtbtnReciveEMails;
	private Button			swtbtnNewEMail;
	private List			swtlEMailsList;
	private Label			swtlblEMails;
	
	/**
	 * Auto-generated main method to display this
	 * org.eclipse.swt.widgets.Composite inside a new Shell.
	 */
	public static void main(String[] args) {
		showGUI();
	}
	
	/**
	 * Auto-generated method to display this org.eclipse.swt.widgets.Composite
	 * inside a new Shell.
	 */
	public static void showGUI() {
		Display display = Display.getDefault();
		Shell shell = new Shell(display);
		EMailCompositeTab inst = new EMailCompositeTab(shell, SWT.NULL);
		Point size = inst.getSize();
		shell.setLayout(new FillLayout());
		shell.layout();
		if (size.x == 0 && size.y == 0) {
			inst.pack();
			shell.pack();
		} else {
			Rectangle shellBounds = shell.computeTrim(0, 0, size.x, size.y);
			shell.setSize(shellBounds.width, shellBounds.height);
		}
		shell.open();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
	}
	/**
	 * Constructor basado en el constructor super. 
	 */
	public EMailCompositeTab(org.eclipse.swt.widgets.Composite parent, int style) {
		super(parent, style);
		initGUI();
	}
	
	private void initGUI() {
		try {
			this.setLayout(new FormLayout());
			this.setSize(397, 252);
			{
				swtbtnReciveEMails = new Button(this, SWT.PUSH | SWT.CENTER);
				FormData swtbtnReciveEMailsLData = new FormData();
				swtbtnReciveEMailsLData.width = 245;
				swtbtnReciveEMailsLData.height = 34;
				swtbtnReciveEMailsLData.right = new FormAttachment(998, 1000, 0);
				swtbtnReciveEMailsLData.top = new FormAttachment(0, 1000, 0);
				swtbtnReciveEMailsLData.left = new FormAttachment(500, 1000, 0);
				swtbtnReciveEMails.setLayoutData(swtbtnReciveEMailsLData);
				swtbtnReciveEMails.setText("Recive e-Mails");
				swtbtnReciveEMails.addMouseListener(new MouseAdapter() {
					@Override
					public void mouseUp(MouseEvent evt) {
						swtbtnReciveEMailsMouseUp(evt);
					}
				});
				swtbtnReciveEMails
						.setImage(new Image(Display.getCurrent(),
								"resource/tango-icon-theme-0.8.90/32x32/actions/mail-send-receive.png"));
			}
			{
				swtbtnNewEMail = new Button(this, SWT.PUSH | SWT.CENTER);
				FormData swtbtnNewEMailLData = new FormData();
				swtbtnNewEMailLData.width = 245;
				swtbtnNewEMailLData.height = 34;
				swtbtnNewEMailLData.right = new FormAttachment(500, 1000, 0);
				swtbtnNewEMailLData.top = new FormAttachment(0, 1000, 0);
				swtbtnNewEMailLData.left = new FormAttachment(1, 1000, 0);
				swtbtnNewEMail.setLayoutData(swtbtnNewEMailLData);
				swtbtnNewEMail.setText("New e-Mail");
				swtbtnNewEMail.addMouseListener(new MouseAdapter() {
					@Override
					public void mouseUp(MouseEvent evt) {
						swtbtnNewEMailMouseUp(evt);
					}
				});
				swtbtnNewEMail
						.setImage(new Image(Display.getCurrent(),
								"resource/tango-icon-theme-0.8.90/32x32/actions/mail-message-new.png"));
				
			}
			{
				swtlblEMails = new Label(this, SWT.NONE);
				FormData swtlblEMailsLData = new FormData();
				swtlblEMailsLData.width = 490;
				swtlblEMailsLData.height = 16;
				swtlblEMailsLData.right = new FormAttachment(998, 1000, 0);
				swtlblEMailsLData.left = new FormAttachment(1, 1000, 0);
				swtlblEMailsLData.top = new FormAttachment(0, 1000, 33);
				swtlblEMails.setLayoutData(swtlblEMailsLData);
				swtlblEMails.setText("Your e-Mails:");
				swtlblEMails.setAlignment(SWT.CENTER);
			}
			{
				FormData swtlEMailsListLData = new FormData();
				swtlEMailsListLData.width = 488;
				swtlEMailsListLData.height = 146;
				swtlEMailsListLData.left = new FormAttachment(0, 1000, 0);
				swtlEMailsListLData.top = new FormAttachment(0, 1000, 55);
				swtlEMailsListLData.bottom = new FormAttachment(1002, 1000, 0);
				swtlEMailsListLData.right = new FormAttachment(1001, 1000, 0);
				swtlEMailsList = new List(this, SWT.NONE);
				swtlEMailsList.setLayoutData(swtlEMailsListLData);
				swtlEMailsList.addMenuDetectListener(new MenuDetectListener() {
					public void menuDetected(MenuDetectEvent evt) {
						swtlEMailsListMenuDetected(evt);
					}
				});
			}
			
			{
				emailsPupupMenu = new EMailPopUpMenu(getShell(), SWT.POP_UP);
				emailsPupupMenu.setBoss(swtlEMailsList);
			}
			
			this.layout();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void sendEMail(String recipient) {
		CommSendEMailDialog dialog = new CommSendEMailDialog(this.getShell(),
				this.getStyle());
		dialog.setEMailRecipient(recipient);
		dialog.open();
	}
	
	private void swtbtnNewEMailMouseUp(MouseEvent evt) {
		sendEMail(null);
	}
	
	private void swtbtnReciveEMailsMouseUp(MouseEvent evt) {
		this.swtlEMailsList.removeAll();
		TuristAssistant.mailClient.reciveEMails();
		emails = new Vector<EMail>(TuristAssistant.mailClient
				.getRecivedEMailsOrderBySender());
		for (EMail email : emails) {
			String eMS = email.getSender() + " | " + email.getSubject();
			this.swtlEMailsList.add(eMS);
		}
	}
	
	private void swtlEMailsListMenuDetected(MenuDetectEvent evt) {
		swtlEMailsList.getMenu().setEnabled(false);
		if (this.swtlEMailsList.getSelectionIndex() >= 0) {
			this.emailsPupupMenu.setEMail(emails.get(this.swtlEMailsList
					.getSelectionIndex()));
			swtlEMailsList.getMenu().setEnabled(true);
		}
	}
}
